/*
 * Copyleft 2012 Power by Colen.
 *
 * Project: app-crm
 * Date: Mar 18, 2012
 */
package com.crm.business.web.cust.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.business.web.cust.service.CustomerService;
import com.crm.common.web.controller.WebCommonController;

/**
 * Customer Controller
 *
 * @author Colen
 *
 */
@Controller("cust_customerController")
public class CustomerController extends WebCommonController {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(CustomerController.class);

    /**
     * Show Customer Main Page
     *
     * @return main page
     */
    @RequestMapping("show_cust.shtm")
    public ModelAndView show(HttpServletRequest req) {
        String module = req.getParameter("module");
        ModelAndView mv = null;
        if (MODULE_MODIFY.equals(module)) {
            mv = showEdit(req);
        } else {
            mv = createOpenPageMV(req);
        }
        mv.setViewName("cust/p00_cust_" + module + ".jsp");
        return mv;
    }


    /**
     * Edit customer
     *
     * @param req HttpServletRequest
     */
    @SuppressWarnings("unchecked")
    private ModelAndView showEdit(HttpServletRequest req) {
        LOG.info("open edit coustomer info beging...");

        // request parameters
        Map<String, Object> params = getReqParams(req);
        
        this.service.showEdit(params);

        ModelAndView mv = createOpenPageMV(req);
        mv.addAllObjects((Map<String, Object>) params.get("custInfo"));
        mv.setViewName("cust/p00_cust_" + req.getParameter("module") + ".jsp");
        
        LOG.info("open edit coustomer info end.");
        
        return mv;
    }
    
    /**
     * query customer info
     *
     * @param req http request
     * @param resp http response
     */
    @RequestMapping("cust/edit.shtm")
    public void edit(HttpServletRequest req, HttpServletResponse resp) {
        LOG.info("open edit coustomer info beging...");
        
        // request parameters
        Map<String, Object> params = getReqParams(req);
        this.service.doEdit(params);

        outputJSONDefault(resp);
        
        LOG.info("open edit coustomer info end.");
    }

    /**
     * query customer info
     *
     * @param req http request
     * @param resp http response
     */
    @RequestMapping("cust/query.shtm")
    public void query(HttpServletRequest req, HttpServletResponse resp) {
        LOG.info("query coustomer info beging...");

        // request parameters
        Map<String, Object> params = getReqParams(req);

        // query
        this.service.doQuery(params);

        Map<String, Object> output = getDefaultResult();
        output.put("custInfos", params.get("custInfos"));
        outputJSON(resp, output);

        LOG.info("query coustomer info finshed.");
    }

    /**
     * Add customer
     *
     * @param req HttpServletRequest
     * @param resp HttpServletResponse
     */
    @RequestMapping("cust/add.shtm")
    public void add(HttpServletRequest req, HttpServletResponse resp) {
        LOG.info("add coustomer info beging...");

        // request parameters
        Map<String, Object> params = getReqParams(req);

        // add
        this.service.doAdd(params);

        Map<String, Object> output = getDefaultResult();
        outputJSON(resp, output);

        LOG.info("add coustomer info finshed.");
    }


    /** Main Service */
    @Resource(name = "cust_customerService")
    private CustomerService service = null;
}
